package org.edu.modules.stu.shet.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.shet.dto.VeStuAssocMemberDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.shet.entity.VeStuAssocMember;
import org.edu.modules.stu.shet.service.VeStuAssocMemberService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"社团成员"})
@RestController
@RequestMapping("/stu/veStuAssocMember")
@Slf4j
public class VeStuAssocMemberController{

    @Autowired
    private VeStuAssocMemberService veStuAssocMemberService;

    @ApiOperation("分页查询社团成员")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuAssocMember> page) {
        QueryWrapper<VeStuAssocMember> queryWrapper =  page.buildQueryWrapper(VeStuAssocMember.class);
        return Result.OK(veStuAssocMemberService.page(page, queryWrapper));
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @GetMapping("/findByAssocId")
    public Result<?> findByAssocId(String assocId) {
        return Result.OK(veStuAssocMemberService.findAllSql(assocId));
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuAssocMember> page, HttpServletResponse resp) {
        QueryWrapper<VeStuAssocMember> queryWrapper =  page.buildQueryWrapper(VeStuAssocMember.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("社团id","assocId"));
        list.add(new ExcelPo("学生id","stuId"));
        list.add(new ExcelPo("成员角色id","roleId"));
        list.add(new ExcelPo("标签","label"));
        list.add(new ExcelPo("入团审核状态  0=待审核  1=通过  2=不通过","auditStatus"));
        list.add(new ExcelPo("入团时间","addTime"));
                PoiUtils.downloadFile(resp,veStuAssocMemberService.page(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "社团成员-分页列表查询")
    @ApiOperation(value="社团成员-分页列表查询", notes="社团成员-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuAssocMember entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuAssocMember> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuAssocMember> page = new PageDto<VeStuAssocMember>(pageNo, pageSize);
        IPage<VeStuAssocMember> pageList = veStuAssocMemberService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeStuAssocMember dto = veStuAssocMemberService.findDtoById(id);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuAssocMemberDto dto) {
        veStuAssocMemberService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuAssocMemberDto dto) {
        veStuAssocMemberService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veStuAssocMemberService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        LambdaUpdateWrapper<VeStuAssocMember> updateWrapper = new LambdaUpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
       //     updateWrapper.set(VeStuAssocMember::getStatus,statusDto.getStatus());
            updateWrapper.in(VeStuAssocMember::getId,statusDto.getIds());
            veStuAssocMemberService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }




}
